<template>
  <div class="update-pwd">
    <ul>
      <li>
        <label>
          <span>初始密码：</span>
          <input type="password" v-model="pwdData.oldPwd" />
        </label>
      </li>
      <li>
        <label>
          <span>密码：</span>
          <input type="password" v-model="pwdData.newPwd" />
        </label>
      </li>
      <li>
        <label>
          <span>确认密码：</span>
          <input type="password" v-model="pwdData.newPwd2" />
        </label>
      </li>
      <li><button @click="updatePwd">修改密码</button></li>
    </ul>
  </div>
</template>

<script>
import { updatePwdApi } from "../../../api";
export default {
  name: "UpdatePwd",
  data() {
    return {
      key: true,
      pwdData: {
        oldPwd: "",
        newPwd: "",
        newPwd2: "",
      },
    };
  },
  methods: {
    message(ms) {
      if (/.*oldPwd.*newPwd.*/.test(ms)) {
        this.key = true;
        return "旧密码和新密码格式不正确！";
      }
      if (/.*oldPwd.*/.test(ms)) {
        this.key = true;
        return "旧密码格式不正确！";
      }
      if (/.*newPwd.*/.test(ms)) {
        this.key = true;
        return "新密码格式不正确！";
      }
      return ms;
    },
    updatePwd() {
      if (this.key) {
        this.key = false;
      } else {
        return;
      }
      if (
        this.pwdData.oldPwd === "" &&
        this.pwdData.newPwd === "" &&
        this.pwdData.newPwd2 === ""
      ) {
        this.key = true;
        this.$bus.$emit("showTips", "初始密码，新密码和确认密码不能为空！!");
        return;
      } else if (this.pwdData.oldPwd === "" && this.pwdData.newPwd === "") {
        this.key = true;
        this.$bus.$emit("showTips", "初始密码和新密码不能为空！");
        return;
      } else if (this.pwdData.oldPwd === "" && this.pwdData.newPwd2 === "") {
        this.key = true;
        this.$bus.$emit("showTips", "初始密码和确认密码不能为空！");
        return;
      } else if (this.pwdData.newPwd === "" && this.pwdData.newPwd2 === "") {
        this.key = true;
        this.$bus.$emit("showTips", "新密码和确认密码不能为空！");
        return;
      } else if (this.pwdData.oldPwd === "") {
        this.key = true;
        this.$bus.$emit("showTips", "初始密码不能为空！");
        return;
      } else if (this.pwdData.newPwd === "") {
        this.key = true;
        this.$bus.$emit("showTips", "新密码不能为空！");
        return;
      } else if (this.pwdData.newPwd2 === "") {
        this.key = true;
        this.$bus.$emit("showTips", "确认密码不能为空！");
        return;
      } else if (this.pwdData.newPwd !== this.pwdData.newPwd2) {
        this.key = true;
        this.$bus.$emit("showTips", "新密码和确认密码不一致！");
        return;
      }
      if (!confirm("是否修改")) return (this.key = true);
      updatePwdApi(
        (response) => {
          if (response.data.status === 1) {
            this.$bus.$emit("showTips", "密码修改成功！请重兴登录！");
            this.key = true;
            this.$store.commit("user/QUIT");
            this.$router.push("/home");
          } else {
            this.$bus.$emit("showTips", this.message(response.data.message));
            this.key = true;
          }
        },
        (err) => {
          this.$bus.$emit("showTips", "出错了！请检查网络是否连接！");
          this.key = true;
          console.log(err);
        },
        {
          oldPwd: this.pwdData.oldPwd,
          newPwd: this.pwdData.newPwd2,
        }
      );
    },
  },
};
</script>

<style lang="stylus" scoped>
.update-pwd {
  width: 80%;
  color: #aaa;
  font-size: 14px;

  ul {
    padding-top: 20px;
    line-height: 50px;
  }

  label>span:first-child {
    display: inline-block;
    width: 90px;
    vertical-align: middle;
  }

  input {
    width: 180px;
    height: 22px;
    padding: 0 6px;
    border: 1px solid #999;
    vertical-align: middle;
  }

  button {
    padding: 6px 20px;
    border: none;
    margin-left: 120px;
    border-radius: 4px;
    font-size: 14px;
    background-color: #fe4d1b;
    color: #fff;
  }
}
</style>